package example.demo.utils;

import example.demo.customerValidator.sequenceprovider.EmployeeGroupSequenceProvider;
import org.springframework.util.ClassUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class ClassUtil {

    /**
     * class EmployeeGroupSequenceProvider implements DefaultGroupSequenceProvider<Employee>
     * 拿到EmployeeGroupSequenceProvider泛型的Employee.class对象
     */
    public static Class getOneClass(Class c) {

        Type[] genericInterfaces = c.getGenericInterfaces();
        Type genericSuperclass = c.getGenericSuperclass();
        Type genericInterface = genericInterfaces[0];
        Class clazz = null;
        if (genericInterface instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
            Type[] typeArguments = parameterizedType.getActualTypeArguments();
            Type typeArgument = typeArguments[0];
            if (typeArgument instanceof Class) {
                clazz = (Class) typeArgument;
            } else {
                String typeName = typeArgument.getTypeName();
                try {
                    clazz = Class.forName(typeName);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        return clazz;
    }

    public static void main(String[] args) {
        Class<EmployeeGroupSequenceProvider> employeeGroupSequenceProviderClass = EmployeeGroupSequenceProvider.class;
        Class oneClass = getOneClass(employeeGroupSequenceProviderClass);
        System.out.println(oneClass.getName());
    }
}
